package com.yl.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.Fangjian;
import com.yl.auth.entity.Fangjianleixing;
import com.yl.auth.entity.Yudingdengji;
import com.yl.auth.model.entity.Chuangwei;
import com.yl.auth.model.entity.Remid;
import com.yl.auth.model.entity.RuyuanfeiBean;
import com.yl.auth.service.*;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


/**
 * @author 王帅洋
 * @belongsProject: ln-ylHouse
 * @belongsPackage: com.ln.auth.controller
 * @Date 2023/6/16 15:58
 * @Description: TODO
 * @Version: 1.0
 */
@Api(tags = "财务收支-入院费用核定管理")
@RestController
@RequestMapping("/admin/caiwushouzhi/ruyuanfeiyong")
@EnableScheduling //开启定时任务
public class RuyuanfeiController {
    @Resource
    private RuyuanfeiService ruyuanfeiService;
    @Resource
    private LaorenService laorenService;
    @Resource
    private YudingdengjiService yudingdengjiService;
    @Resource
    private ChuangweiService chuangweiService;
    @Resource
    private FangjianService fangjianService;
    @Resource
    private FangjianleixingService fangjianleixingService;
    @Resource
    private RemidService remidService;

    @ApiOperation("分页条件查询")
    @GetMapping("/{page}/{limit}")
    public Result selepage(@PathVariable Long page, @PathVariable Long limit, RuyuanfeiBean ruyuanfeiBean) {
        Page<RuyuanfeiBean> page1 = new Page<>(page, limit);
        IPage<RuyuanfeiBean> page2 = ruyuanfeiService.selectpage(page1, ruyuanfeiBean);
        return Result.ok(page2);
    }

    @ApiOperation("根据id查询")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id) {
        RuyuanfeiBean ruyuanfeiBean = ruyuanfeiService.getById(id);
        return Result.ok(ruyuanfeiBean);
    }

    @ApiOperation("根据id修改信息")
    @PostMapping("/updateById")
    public Result updateById(@RequestBody RuyuanfeiBean ruyuanfeiBean) {
        ruyuanfeiBean.setTotal((ruyuanfeiBean.getBedPrice() + ruyuanfeiBean.getNursePrice() + ruyuanfeiBean.getFoodPrice() + ruyuanfeiBean.getCashPledge() + ruyuanfeiBean.getDisposablePrice()) * ruyuanfeiBean.getEarnestPrice());
//        ruyuanfeiBean.setState("1");
        ruyuanfeiService.updateById(ruyuanfeiBean);
        return Result.ok();
    }

    @ApiOperation("添加老人入院时的费用")
    @PostMapping("/saveLaorenRuYuan/{laorenId}")
    public Result saveLaorenRuYuan(@RequestBody RuyuanfeiBean ruyuanfeiBean, @PathVariable Long laorenId) {
        //订单总费用 根据总天数进行想加
        Yudingdengji yudingdengji = yudingdengjiService.getById(laorenId);
        Chuangwei chuangwei = chuangweiService.getById(yudingdengji.getBedId());
        Fangjian fangjian = fangjianService.getById(chuangwei.getRoomId());
        Fangjianleixing fangjianleixing = fangjianleixingService.getById(fangjian.getRoomType());
        ruyuanfeiBean.setNursePrice(ruyuanfeiBean.getHulifei() * ruyuanfeiBean.getTianshu());
        ruyuanfeiBean.setFoodPrice(ruyuanfeiBean.getShanshifei() * ruyuanfeiBean.getTianshu());
        ruyuanfeiBean.setBedPrice(fangjianleixing.getPrice() * ruyuanfeiBean.getTianshu());
        ruyuanfeiBean.setLaorenId(yudingdengji.getLaorenId());
        ruyuanfeiBean.setTotal(ruyuanfeiBean.getBedPrice() + ruyuanfeiBean.getFoodPrice() + ruyuanfeiBean.getNursePrice() + ruyuanfeiBean.getDisposablePrice() + ruyuanfeiBean.getCashPledge());
        //入院总费用时间
        //创建当前时间
        LocalDate localDate = LocalDate.now();
        //根据天数计算时间
        LocalDate current = localDate.plusDays((long) ruyuanfeiBean.getTianshu());
        LocalDate localDate1 = localDate.plusDays(ruyuanfeiBean.getVouchTime());
        ruyuanfeiBean.setZlocalDate(current);
        ruyuanfeiBean.setNewDate(localDate);
        ruyuanfeiBean.setHlocalDate(localDate1);
        System.out.println("---------------------------------------------" + ruyuanfeiBean.getZlocalDate());
        System.out.println("---------------------------------------------" + ruyuanfeiBean.getNewDate());
        //入院费用核定 根据核定天数进行相加
        ruyuanfeiBean.setBedPrices(fangjianleixing.getPrice() * ruyuanfeiBean.getVouchTime());
        ruyuanfeiBean.setShanshiPrice(ruyuanfeiBean.getShanshifei() * ruyuanfeiBean.getVouchTime());
        ruyuanfeiBean.setHuliPrice(ruyuanfeiBean.getHulifei() * ruyuanfeiBean.getVouchTime());
        ruyuanfeiBean.setZongjia(ruyuanfeiBean.getBedPrices() + ruyuanfeiBean.getShanshiPrice() + ruyuanfeiBean.getHuliPrice() + ruyuanfeiBean.getDisposablePrice() + ruyuanfeiBean.getCashPledge());
        ruyuanfeiService.save(ruyuanfeiBean);
        return Result.ok();
    }

    @ApiOperation("订单确定功能")
    @GetMapping("/getDingDan/{id}")
    public Result getDingDan(@PathVariable Integer id) {
        Yudingdengji yudingdengji = yudingdengjiService.getById(id);
        Long laorenId = yudingdengji.getLaorenId();
        LambdaQueryWrapper<RuyuanfeiBean> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RuyuanfeiBean::getLaorenId, laorenId);
        RuyuanfeiBean ruyuanfeiBean = ruyuanfeiService.getOne(wrapper);
        System.out.println("---------------------------------------------" + ruyuanfeiBean.getNewDate());
        System.out.println("---------------------------------------------" + ruyuanfeiBean.getZlocalDate());
        return Result.ok(ruyuanfeiBean);
    }


    @ApiOperation(value = "订单确定上传协议")
    @PostMapping("/getUpdateUrl")
    public Result getUpdateUrl(@RequestBody RuyuanfeiBean ruyuanfeiBean) {
        boolean b = ruyuanfeiService.updateById(ruyuanfeiBean);
        if (b) {
            return Result.ok("上传成功");
        } else {
            return Result.fail("上传失败");
        }
    }


    //定时减少天数
    @Scheduled(cron = "0 0 0  * * ?")
    public void reduceSky() {

        List<RuyuanfeiBean> list = ruyuanfeiService.list();

        for (RuyuanfeiBean ruyuanfeiBean : list) {
            if (ruyuanfeiBean.getState() == 0) {

                ruyuanfeiBean.setVouchTime(ruyuanfeiBean.getVouchTime() - 1);
                ruyuanfeiService.updateById(ruyuanfeiBean);
                if (ruyuanfeiBean.getVouchTime() <= 7) {
                    //获取到入院核定费用管理的id
                    Long id = ruyuanfeiBean.getId();
//                    System.err.println(id);
                    //根据入院核定费用管理的id查询整个入院核定费用管理表
                    RuyuanfeiBean vouchById = ruyuanfeiService.getByIds(id);
                    System.err.println(vouchById);
                    //根据入院核定费用管理的id查询整个入院核定费用管理表得到关联老人的id
                    Long elderlyId = vouchById.getLaorenId();
                    System.err.println(elderlyId);
                    //实例化服务提醒对象
                    Remid remid = new Remid();
                    //把获取到的关联老人的id存入到服务提醒表中
                    remid.setId(elderlyId);
                    //设置服务提醒的内容
                    remid.setCid("老人核定费用天数还有" + ruyuanfeiBean.getVouchTime() + "天到期，请及时缴纳");
                    remidService.save(remid);
                }
            }
        }
        System.out.println("执行定时任务" + LocalDateTime.now());
    }
}
